package net.lbtech.service;

import net.lbtech.util.CommonUtil;
import net.lbtech.util.WifiDataFormatUtil;
import net.lbtech.util.db.DbBuilder;
import net.lbtech.util.exception.SaveIndoorAirException;

import java.util.HashMap;
import java.util.Map;


public class SaveIndoorAir {
    public static byte[]  saveIndoorAir(byte[] data) throws SaveIndoorAirException {
        int rows = 0;
        byte[] returnByte;
        Map dataMap =analysisByteArray(data);
        sqlIaqDataInsert(dataMap);
        sqlIaqConfigSave(dataMap);
        returnByte = WifiDataFormatUtil.trueReturn((byte)0x83);
        return returnByte;
    }

    /**解析byte数组以map储存
    0xF5(0) 0x03(1) AA(2) BB(3) CC(4) DD(5) EE(6) FF(7) GG(8) HH(9) II(10) JJ(11) KK(12) LL MM NN OO PP QQ CRC
    AA(*256),BB：PM2.5数值
    CC(*256),DD:PM10数值
    EE(*256),FF：甲醛数值
    GG:温度
    HH：湿度
    II: PM25超标警告值
    JJ: 甲醛超标警告值
    KK: 蜂鸣器器开（01）关（00）
    LL,MM,NN,OO,PP,QQ: WIFI模块的MAC地址*/
    public static Map<String, Object> analysisByteArray(byte[] data){
        Map<String, Object> dataMap=new HashMap<String,Object>();
        dataMap.put("pm25",data[2]*256+data[3]);//pm2.5
        dataMap.put("pm10",data[4]*256+data[5]);//pm10
        dataMap.put("formaldehyde",data[6]*256+data[7]);//甲醛
        dataMap.put("temperature",data[8]);//温度
        dataMap.put("humidity",data[9]);//湿度
        dataMap.put("pm25_alarm",data[10]);//pm2.5超标警告值
        dataMap.put("formaldehyde_alam",data[11]);//甲醛超标警告值
        dataMap.put("is_voice",data[12]);//蜂鸣器器开（01）关（00）
        dataMap.put("mac",getMacString(data));//WIFI模块的MAC地址
        return dataMap;
    }

    //保存室内数据
    public static int sqlIaqDataInsert(Map dataMap){
        String mac = dataMap.get("mac").toString();
        String pm25 = dataMap.get("pm25").toString();
        String pm10 = dataMap.get("pm10").toString();
        String formaldehyde =dataMap.get("formaldehyde").toString();
        String humidity = dataMap.get("humidity").toString();
        String temperature = dataMap.get("temperature").toString();
        int row =DbBuilder.save("insert indoor_air(mac,pm25,pm10,formaldehyde,humidity,temperature)values(?,?,?,?,?,?)",
                mac,pm25,pm10,formaldehyde,humidity,temperature);
        return row;
    }

    //保存空气检测仪设置
    public static int sqlIaqConfigSave(Map dataMap){
        String pm25_alarm = dataMap.get("pm25_alarm").toString();
        String formaldehyde_alam = dataMap.get("formaldehyde_alam").toString();
        String is_voice = dataMap.get("is_voice").toString();
        String mac =dataMap.get("mac").toString();
        int rows = 0;
        int count = DbBuilder.getCount("select count(*) from iaq_instrument_config where mac = ?",mac);
        if(count>=1){
            rows = DbBuilder.update("update iaq_instrument_config set pm25_alarm =?,formaldehyde_alam=?,is_voice=? where mac = ?",pm25_alarm,formaldehyde_alam,is_voice,mac);
        }else {
            DbBuilder.save("insert iaq_instrument_config(pm25_alarm,formaldehyde_alam,is_voice,mac)values(?,?,?,?)",
                    pm25_alarm,formaldehyde_alam,is_voice,mac);
        }
        return rows;
    }

    //拼接mac地址
    public static String getMacString(byte[] data){
        String mac = "";
        for (int i = 13; i < 18; i++) {
            mac=mac+ CommonUtil.toHex(data[i]);
        }
        System.out.println("mac "+mac);
        return mac;
    }



}